$(function(){
	$("#businessDatagrid").datagrid({
	    toolbar:'#businessTolbar',
	})
	var dataJson = []
	$.ajax({
		url:$WEB_ROOT_PATH+"/Statistics/statistics.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"leaseStartTime":'',"leaseEndTime":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.statisticsList;
		}
	})
	
	chartOpen(dataJson);
})

function chartOpen(dataJson){
	var type = [];
	var count = [];
	for (var i = 0; i < dataJson.length; i++) {
		type[i] = dataJson[i].clothingType;
		
	}
	type = unique(type);
	
	for (var i = 0; i < type.length; i++) {
		var a = 0;
		for (var j = 0; j < dataJson.length; j++) {
			if(type[i] == dataJson[j].clothingType){
				a += dataJson[j].returnCount;
			}
		}
		count[i] = a;
	}
	for(var i in type){
		type [i] = typeToName(type[i],clothingType)
	}

	
	var myChart = echarts.init(document.getElementById('main'));
	option = {
		    color: ['#3398DB'],
		    title : {
		        text: '归还统计',
		    },
		    tooltip : {
		        trigger: 'axis',
		        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		        }
		    },
		    toolbox: {
		        show : true,
		        feature : {
		            dataView : {show: true, readOnly: false},
		            magicType : {show: true, type: ['line', 'bar']},
		            restore : {show: true},
		            saveAsImage : {show: true}
		        }
		    },
		    calculable : true,
		    grid: {
		        left: '3%',
		        right: '4%',
		        bottom: '3%',
		        containLabel: true
		    },
		    xAxis : [
		        {
		            name:'分类',
		        	type : 'category',
		            data : type,
		            axisTick: {
		                alignWithLabel: true
		            }
		        }
		    ],
		    yAxis : [
		        {
		            name:'数量',
		        	type : 'value'
		        }
		    ],
		    series : [
		        {
		            name:'归还数量',
		            type:'bar',
		            barWidth: '60%',
		            data:count
		        }
		    ]
		};
	myChart.setOption(option);
}

function doSearch(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Statistics/statistics.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"leaseStartTime":$("#startTime").datebox("getValue"),"leaseEndTime":$("#endTime").datebox("getValue")}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.statisticsList;
			chartOpen(dataJson);
		}
	})
}

function unique(arr){
	
	var tmp = new Array();
	for(var i in arr){

		if(tmp.indexOf(arr[i])==-1){
			tmp.push(arr[i]);
		}
	}
	
	return tmp;
}